package framework.gui;

import java.awt.*;
import java.io.*;
import javax.swing.*;

import nl.hanze.t23i.gamemodule.extern.*;
import framework.controller.*;
import framework.model.*;
import framework.utils.UserPreferences;
import framework.view.*;
import framework.ai.*;

public class GameGUI extends GUI {

	private final String GUI_NAME = "Game v0.1";
	private final int GUI_WIDTH = 600, GUI_HEIGHT = 600;
	
	private String gameType, playerOne, playerTwo;
	
	private AbstractAIModule AIModule;
	private GameModel gameModel;
	private GameController gameController;
	private BoardGameView gameView;
	private ForfeitView forfeitView;
	private AIview aiView;
	
	public GameGUI(String gameType, String playerOne, String playerTwo) {
		
		this.setGameType(gameType);
		this.setPlayerOne(playerOne);
		this.setPlayerTwo(playerTwo);
		this.setAIModule();
		
		this.init();
		this.setModels();
		this.setControllers();
		this.setViews();
		this.setPanels();
		this.setVisible(true);
	}
	
	private void setPlayerTwo(String playerTwo) {
		this.playerTwo = playerTwo;
	};
	
	private void setPlayerOne(String playerOne) {
		this.playerOne = playerOne;
	};
	
	private void setGameType(String gameType) {
		this.gameType = gameType;
	};
	
	private void setAIModule() {
		switch(this.gameType.replace(" ", "")) {
			case "TicTacToe" :
				this.AIModule = new TicTacToeAI();
			break;
			case "Reversi" :
				this.AIModule = new ReversiAI();
			break;
			default : 
				this.AIModule = new BasicAI(this.gameType);
			break;
		};
	};
	
	/**
	 * @method init
	 * Set frame configurations
	 */
	private void init() {
		this.setMinimumSize(new Dimension(200,200));
		this.setTitle(GUI_NAME + ": " + this.gameType);
		this.setSize(GUI_WIDTH, GUI_HEIGHT);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setLayout(new BorderLayout(0,0));
	}
	
	/**
	 * @method setModels
	 */
	private void setModels() {
		this.gameModel = new GameModel();
		this.gameModel.setAIModule(this.AIModule);
		this.gameModel.setPlayer(this.playerOne, null);
		this.gameModel.setPlayer(this.playerTwo, null);
		this.gameModel.updatePlayerIndex();
	};
	
	/**
	 * @method setControllers
	 */
	private void setControllers() {
		this.gameController = new GameController(this.gameModel);
	};
	
	/**
	 * @method setViews
	 */
	private void setViews() {
		this.gameView = new BoardGameView(this.gameModel, this.gameController);
		this.forfeitView = new ForfeitView(this.gameModel, this.gameController);
		this.aiView = new AIview(this.gameModel, this.gameController);
	};
	
	/**
	 * @method setPanels
	 */
	private void setPanels() {
		super.add(this.aiView, BorderLayout.NORTH);
		super.add(this.gameView, BorderLayout.CENTER);
		super.add(this.forfeitView, BorderLayout.SOUTH);
	};
	
}
